import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router/index'
// import Cookies from 'js-cookie'
// const proxy = {
//   baseURL: process.env.VUE_APP_BASE_API
// }
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  // baseURL: '/api/10559/door/doorDataJson.json',
  timeout: 50000
  // withCredentials: true
})
service.interceptors.request.use(
  config => {
    // console.log(window.name)
    // if (window.name) {
    //   config.headers.Authorization = Cookies.get(window.name)
    // } else {
    // }

    // if (Cookies.get('visualLoginKey')) {
    //   config.headers.Authorization = Cookies.get(Cookies.get('visualLoginKey'))
    // } else {

    // }

    // config.headers['App-Name'] = 'polaris'
    // config.headers.Authorization = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MzA2NjY3NjYsImxvZ2luIjoxLCJyZWFsX25hbWUiOiIiLCJyb2xlX2lkIjo2Niwicm9sZV9uYW1lIjoic2Nob29sX21hbmFnZXIiLCJyb2xlX29yZ19jb2RlIjoiMTA1NTciLCJzY2hvb2xfY29kZSI6IjEwNTU3IiwidXNlcl9pZCI6MjM0LCJ1c2VyX25hbWUiOiIxMDAwMCJ9.FgVgIvsgfQLkZV-uTpEeskPYNvUui_O_3NxRyBxM-v6WXfAvVYEqZS7twtahsOUDqQVRti12aXGqeISoOEJADLrp0C6QLg2jphszNqxm06NsrmCy6B_GCg7wtlHFIawNWCN70BWAocrVuQCUOOIXr_7N8G3be-ksifn-t5emoFrP6ldJSDiJHr7n4zNQBnCye2XZ7iNDJ1BmI3ARtji5cWINluYQ3rE2MjZ8MB0V1UVW-ZsZ95mW1yBg95vxYaFzJmbUadqGEBJvou2d5C-wYSjt_DZC-mHJ1ctcgjeGmfJ4Ojeue2uSVXer0cs8N5hSrbNDRFye3AGHVNSHpatApA'
    // config.headers['Content-Type'] = 'multipart/form-data'
    // config.headers.Authorization = window.localStorage.getItem('token')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const { data, status } = response
    if (status !== 200) {
      Message({
        message: data.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    }
    return data
  },
  error => {
    console.log(error)
    const response = error.response
    if (!response) {
      router.push({ path: '*' })
      Message({
        message: '网络错误，请联系管理员！',
        type: 'error',
        duration: 5 * 1000
      })
    }
    return error
  }
)

export default service
